/*
 * Copyright (C) 2021 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.bobo.base.utils;


import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;

import com.bobo.base.BaseApplication;
import com.bobo.base.R;


/**
 * xtoast 工具类
 *
 * @author xuexiang
 * @since 2019-06-30 19:04
 */
public final class XToastUtils {
    private final String TAG = "XToastUtils";


    private XToastUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

//    static {
//        XToast.Config.get()
//                .setAlpha(200)
//                .allowQueue(false);
//    }

    //======普通土司=======//

    @MainThread
    public static void toast(@NonNull CharSequence message) {
        try {
            Toast.makeText(BaseApplication.getInstance(), message, Toast.LENGTH_SHORT).show();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @MainThread
    public static void toast(@StringRes int message) {
        try {
            Toast.makeText(BaseApplication.getInstance(), message, Toast.LENGTH_SHORT).show();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @MainThread
    public static void showCustomToast(String msg) {
        showCustomToast(msg, 0);
    }

    @MainThread
    public static void showCustomToast(String msg, int iconId) {
        Toast toast = new Toast(BaseApplication.getInstance());
        View view = LayoutInflater.from(BaseApplication.getInstance()).inflate(R.layout.layout_toast, null);
        ImageView icon = view.findViewById(R.id.icon);
        if (iconId > 0) {
            icon.setImageResource(iconId);
        }
        TextView tvMsg = view.findViewById(R.id.tvMsg);
        tvMsg.setText(msg);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

}
